'use strict';

const Controller = require('egg').Controller;
const {ObjectId}  = require('mongodb')

class DetailController extends Controller {
  async index() {
    const { ctx } = this;
    var {id} = ctx.request.query
    var db = this.app.mongodb;
    var type = ['box','switchs','gmk','banner','group','discount']
    var res =[]
    try {
      for(let i = 0 ; i < type.length ; i++){
        var arr = await db.collection(type[i]).find({_id:ObjectId(id)}).toArray()
        if(arr.length){
          res = arr
        }
      }
    }catch{
      ctx.body = {
        statu :400,
        msg:'不正确'
      }
    }
    
    if(res.length){
      ctx.body = {
        statu :200,
        res
      }
    }else{
      ctx.body = {
        statu :400,
        msg:'不正确'
      }
    }
  }
}

module.exports = DetailController;
